package com.timerecording.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.timerecording.Enum.DeleteEnum;
import com.timerecording.dao.OperateDao;
import com.timerecording.dao.ReSalaryDao;
import com.timerecording.dao.RecordTimeDao;
import com.timerecording.domain.entity.OperateLogEntity;
import com.timerecording.domain.entity.ReSalaryEntity;
import com.timerecording.domain.entity.ReTeamEntity;
import com.timerecording.domain.entity.RecordTimeEntity;
import com.timerecording.domain.form.ReSalaryForm;
import com.timerecording.domain.form.RecordTimeAddForm;
import com.timerecording.domain.vo.RecordTimeVO;
import com.timerecording.util.R;
import com.timerecording.util.ResponseCodeConst;
import com.timerecording.util.ResponseDTO;
import com.timerecording.util.SmartBeanUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.Date;
import java.util.List;

/**
 * @author smz41
 * @description: TODO
 * @date 2024/4/4
 */

@Service
public class RecordTimeService {



    @Autowired
    private RecordTimeDao recordTimeDao;


    @Autowired
    private ReSalaryDao reSalaryDao;


    @Autowired
    private OperateDao operateDao;

    /** 新增工时记录  修改工时记录
     * @description: TODO
     * @author smz
     * @version 1.0
     * @date 2024/4/4
     */

    public R insOrupdRecordTime(RecordTimeAddForm recordAddForm) throws UnknownHostException {
        RecordTimeEntity recordTimeEntity = SmartBeanUtil.copy(recordAddForm, RecordTimeEntity.class);
        //日志记录
        OperateLogEntity operateLogEntity = new OperateLogEntity();
        operateLogEntity.setCreateTime(new Date());
        operateLogEntity.setSuccessFlag(Boolean.TRUE);
        operateLogEntity.setUpdateTime(new Date());
        operateLogEntity.setUrl("/record/insOrupdRecordTime");
        InetAddress addr = InetAddress.getLocalHost();
        operateLogEntity.setIp(addr.toString());
        if (null == recordAddForm.getRecordId()){
            recordTimeEntity.setDeleteFlag(Boolean.FALSE);
            recordTimeEntity.setCreateTime(new Date());
            recordTimeEntity.setUpdateTime(new Date());
            recordTimeDao.insert(recordTimeEntity);
            //新增日志记录
            operateLogEntity.setContent("新增工时记录 @author smz");
            operateDao.insert(operateLogEntity);
            return R.ok();
        }
        //修改工时记录
        operateLogEntity.setContent("修改工时记录 @author smz");
        recordTimeDao.updateById(recordTimeEntity);
        operateDao.insert(operateLogEntity);
        return R.ok();

    }

    /** 两表查询当前小时工资
     * @description: TODO
     * @author smz
     * @version 1.0
     * @date 2024/4/4
     */

    public ResponseDTO<List<ReSalaryEntity>> queryHourSalary() {
        List<ReSalaryEntity> salaryList = recordTimeDao.queryHourSalary(DeleteEnum.NORMAL.getValue());
        return ResponseDTO.succData(salaryList,"查询成功！");
    }

    /** 两表查询从表班次名称
     * @description: TODO
     * @author smz
     * @version 1.0
     * @date 2024/4/4
     */

    public ResponseDTO<List<ReTeamEntity>> queryTeam() {
        List<ReTeamEntity> teamNameList = recordTimeDao.queryTeam(DeleteEnum.NORMAL.getValue());
        return ResponseDTO.succData(teamNameList,"查询成功！");



    }

    /** 查询工时列表
     * @description: TODO
     * @author smz
     * @version 1.0
     * @date 2024/4/5
     */

    public ResponseDTO<List<RecordTimeVO>> queryRecordTimeList(String recordTime) {
        List<RecordTimeVO> recordTimeList = recordTimeDao.queryRecordTimeList(DeleteEnum.NORMAL.getValue(),recordTime);
        return ResponseDTO.succData(recordTimeList,"查询成功！");
    }

    /** 删除工时记录
     * @description: TODO
     * @author smz
     * @version 1.0
     * @date 2024/4/9
     */

    public R deleteRecordTime(Integer recordId) throws UnknownHostException {
        recordTimeDao.deleteById(recordId);
        //日志记录
        OperateLogEntity operateLogEntity = new OperateLogEntity();
        operateLogEntity.setCreateTime(new Date());
        operateLogEntity.setSuccessFlag(Boolean.TRUE);
        operateLogEntity.setUpdateTime(new Date());
        operateLogEntity.setUrl("/record/deleteRecordTime");
        InetAddress addr = InetAddress.getLocalHost();
        operateLogEntity.setIp(addr.toString());
        //删除日志记录
        operateLogEntity.setContent("删除工时记录 @author smz");
        operateDao.insert(operateLogEntity);
        return R.ok();
    }

    /** 编辑工时
     * @description: TODO
     * @author smz
     * @version 1.0
     * @date 2024/4/20
     */

    public ResponseDTO<List<RecordTimeVO>> eduitRecordTime(Integer recordId) {
        List<RecordTimeVO> list = recordTimeDao.eduitRecordTime(recordId);
        return ResponseDTO.succData(list,"查询成功");
    }

    /** 查询工作天数
     * @description: TODO
     * @author smz
     * @version 1.0
     * @date 2024/5/1
     */

    public int workDayQuery(String recordTime) {
        return recordTimeDao.workDayQuery(DeleteEnum.NORMAL.getValue(),recordTime);
    }

    /** 小时工资单表查询
     * @description: TODO
     * @author smz
     * @version 1.0
     * @date 2024/5/1
     */

    public ResponseDTO<List<ReSalaryEntity>> queryWorkHour() {
        QueryWrapper<ReSalaryEntity> reSalaryEntityQueryWrapper = new QueryWrapper<>();
        reSalaryEntityQueryWrapper.eq("delete_flag",0);
        reSalaryEntityQueryWrapper.orderByDesc("salary_id");
        List<ReSalaryEntity> list = reSalaryDao.selectList(reSalaryEntityQueryWrapper);
        return ResponseDTO.succData(list,"查询成功！！！");
    }

    /** 小时工资表删除 此为逻辑删除不是物理删除 修改deleteFlag为1
     * @description: TODO
     * @author smz
     * @version 1.0
     * @date 2024/5/2
     */

    public R deleteWorkHourById(Integer salaryId) throws UnknownHostException {
        //reSalaryDao.deleteById(salaryId);
        reSalaryDao.updateById(salaryId);
        //日志记录
        OperateLogEntity operateLogEntity = new OperateLogEntity();
        operateLogEntity.setCreateTime(new Date());
        operateLogEntity.setSuccessFlag(Boolean.TRUE);
        operateLogEntity.setUpdateTime(new Date());
        operateLogEntity.setUrl("/record/deleteWorkHourById");
        InetAddress addr = InetAddress.getLocalHost();
        operateLogEntity.setIp(addr.toString());
        //删除日志记录
        operateLogEntity.setContent("小时工资表逻辑删除记录 @author smz");
        operateDao.insert(operateLogEntity);
        return R.ok();
    }

    /** 小时工资状态恢复 delete_flag = 0
     * @description: TODO
     * @author smz
     * @version 1.0
     * @date 2024/7/15
     */

    public R recoverSalaryById(Integer salaryId) throws UnknownHostException {
        reSalaryDao.updateDelectFlagById(salaryId);
        //日志记录
        OperateLogEntity operateLogEntity = new OperateLogEntity();
        operateLogEntity.setCreateTime(new Date());
        operateLogEntity.setSuccessFlag(Boolean.FALSE);
        operateLogEntity.setUpdateTime(new Date());
        operateLogEntity.setUrl("/record/updateDelectFlagById");
        InetAddress addr = InetAddress.getLocalHost();
        operateLogEntity.setIp(addr.toString());
        //删除日志记录
        operateLogEntity.setContent("小时工资表逻辑删除恢复记录 @author smz");
        operateDao.insert(operateLogEntity);
        return R.ok();
    }

    /** 新增或修改小时工资表单表
     * @description: TODO
     * @author smz
     * @version 1.0
     * @date 2024/5/2
     */

    public R insOrupdWorkHour(ReSalaryForm reSalaryForm) throws UnknownHostException {
        ReSalaryEntity reSalaryEntity = SmartBeanUtil.copy(reSalaryForm, ReSalaryEntity.class);
        OperateLogEntity operateLogEntity = new OperateLogEntity();
        operateLogEntity.setCreateTime(new Date());
        operateLogEntity.setSuccessFlag(Boolean.TRUE);
        operateLogEntity.setUpdateTime(new Date());
        InetAddress addr = InetAddress.getLocalHost();
        operateLogEntity.setIp(addr.toString());
        operateLogEntity.setUrl("/record/insOrupdWorkHour");
        if (null == reSalaryEntity.getSalaryId()) {
            //走新增逻辑
            reSalaryEntity.setDeleteFlag(Boolean.FALSE);
            reSalaryEntity.setCreateTime(new Date());
            reSalaryEntity.setUpdateTime(new Date());
            reSalaryDao.insert(reSalaryEntity);
            operateLogEntity.setContent("小时工资表新增记录 @author smz");
            operateDao.insert(operateLogEntity);
        }
        else
           //修改逻辑
            reSalaryEntity.setUpdateTime(new Date());
            reSalaryDao.updateById(reSalaryEntity);
            operateLogEntity.setContent("小时工资表修改记录 @author smz");
            operateDao.insert(operateLogEntity);
            return R.ok();
    }

    /** 编辑详情  回显
     * @description: TODO
     * @author smz
     * @version 1.0
     * @date 2024/5/2
     */

    public ResponseDTO<ReSalaryEntity> eduitWorkHour(Integer salaryId) {
        if (StringUtils.isNotBlank(salaryId.toString())) {
            ReSalaryEntity reSalaryEntity = reSalaryDao.selectById(salaryId);
            return ResponseDTO.succData(reSalaryEntity,"查询成功！！！");
        }
        else
            return ResponseDTO.wrapMsg(ResponseCodeConst.NOT_EXISTS,"查询失败！！！");
    }

    /** 日志记录查询
     * @description: TODO
     * @author smz
     * @version 1.0
     * @date 2024/5/2
     */

    public ResponseDTO<List<OperateLogEntity>> queryLog() {
        QueryWrapper<OperateLogEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("success_flag",1);
        queryWrapper.orderByDesc("operate_log_id");
        queryWrapper.last("limit 10");
        List<OperateLogEntity> list = operateDao.selectList(queryWrapper);
        return ResponseDTO.succData(list,"查询成功！！！");
    }

    /** 导出工时列表
     * @description: TODO
     * @author smz
     * @version 1.0
     * @date 2024/5/2
     */

    public List<RecordTimeEntity> exportExcel() {
        return recordTimeDao.exportExcel();
    }
}
